/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.model;

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import nl.cloudfarming.client.util.DateUtil;

/**
 * A culture is a part of a plot, containing a certain type of crop
 * A culture only exists for the duration of the growing season.
 * Where plots are boundried by natural borders like roads and water,
 * cultures are boundried by the region their crop is sown.
 *
 * Cultures shapes can contains holes
 *
 *
 * @author Timon Veenstra
 */
@NamedQueries(value = {
    @NamedQuery(name = "findAllCultures", query = "select c from Culture c"),
    @NamedQuery(name = "findAllCulturesByDate", query = "select c from Culture c where :date between c.startDate and c.endDate")
})
@Entity
@Table(name = "CULTURE")
public class Culture {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private long id;
    @Column(name = "NAME")
    private String name;
    @Column(name = "WKT", length = 10000)
    private String WKT;
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "PLOT_FILE_ID")
    private PlotFile plotFile;
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "PLOT_ID")
    private Plot plot;
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "START_DATE")
    private Date startDate = DateUtil.minInfinity();
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "END_DATE")
    private Date endDate = DateUtil.plusInfinity();

    public long getId() {
        return id;
    }

    public String getWKT() {
        return WKT;
    }

    public void setWKT(String WKT) {
        this.WKT = WKT;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public PlotFile getPlotFile() {
        return this.plotFile;
    }

    public void setPlotFile(PlotFile plotFile) {
        this.plotFile = plotFile;
    }

    public Plot getPlot() {
        return plot;
    }

    public void setPlot(Plot plot) {
        this.plot = plot;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEnDate(Date endDate) {
        this.endDate = endDate;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Culture other = (Culture) obj;
        if (this.id != other.id) {
            return false;
        }
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        if ((this.WKT == null) ? (other.WKT != null) : !this.WKT.equals(other.WKT)) {
            return false;
        }
        if (this.plotFile != other.plotFile && (this.plotFile == null || !this.plotFile.equals(other.plotFile))) {
            return false;
        }
        if (this.plot != other.plot && (this.plot == null || !this.plot.equals(other.plot))) {
            return false;
        }
        if (this.startDate != other.startDate && (this.startDate == null || !this.startDate.equals(other.startDate))) {
            return false;
        }
        if (this.endDate != other.endDate && (this.endDate == null || !this.endDate.equals(other.endDate))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 73 * hash + (int) (this.id ^ (this.id >>> 32));
        hash = 73 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 73 * hash + (this.WKT != null ? this.WKT.hashCode() : 0);
        hash = 73 * hash + (this.plotFile != null ? this.plotFile.hashCode() : 0);
        hash = 73 * hash + (this.plot != null ? this.plot.hashCode() : 0);
        hash = 73 * hash + (this.startDate != null ? this.startDate.hashCode() : 0);
        hash = 73 * hash + (this.endDate != null ? this.endDate.hashCode() : 0);
        return hash;
    }
}
